package it.tcm.TeacherClassMate.action;

import it.tcm.TeacherClassMate.beans.OrderMainBean;
import it.tcm.TeacherClassMate.facade.OrderMainManageBase;
import it.tcm.TeacherClassMate.form.TemplateForm;
import it.tcm.TeacherClassMate.persistent.DBConnection;
import it.tcm.TeacherClassMate.util.Util;

import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

public class InsertDataTemplateAction extends Action {

	// --------------------------------------------------------- Instance Variables

	// --------------------------------------------------------- Methods


	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * 
	 */

	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) 
	{		
		 logger.info("INSERT DATA TEMPLATE");
		 String idtemplate = null;
		 if (request.getSession().getAttribute(Util.ORDER_MAIN_OBJECT) != null) {
			 OrderMainBean omb = (OrderMainBean)request.getSession().getAttribute(Util.ORDER_MAIN_OBJECT);
			 idtemplate = omb.getTemplateId().toString();
			 // devo cancellare l'ordine e tutta la discendenza...
			 OrderMainManageBase ommb = new OrderMainManageBase();
			 ommb.delete(omb);
			 request.getSession().removeAttribute(Util.ORDER_MAIN_OBJECT);
		 } else {
			 idtemplate = request.getParameter("id");
			 logger.info("idtemplate " + idtemplate);
		 }
		 TemplateForm tform = new TemplateForm();
		 tform.setIdtemplate(idtemplate);
		 request.setAttribute(Util.FORM_TEMPLATE, tform);
		 return mapping.findForward("insertdatatemplate"); 
		 
	}
	
	
}